package im.elephas.fanfouapi;

import im.elephas.fanfouapi.items.StatusItem;
import im.elephas.fanfouapi.items.UserItem;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.LinkedList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

@SuppressWarnings("unused")
public class FanfouXml {

	public static void getDirectMessages(String xml)
			throws XmlPullParserException, IOException {
		Reader rd = new InputStreamReader(new ByteArrayInputStream(
				xml.getBytes()));

		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(rd);

		int eventType = parser.getEventType();

		while (eventType != XmlPullParser.END_DOCUMENT) {
			String node = parser.getName();
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
			case XmlPullParser.START_TAG:
			case XmlPullParser.END_TAG:
			default:
				break;
			}
			eventType = parser.next();
		}
	}

	public static List<StatusItem> getStatuses(String xml)
			throws XmlPullParserException, IOException {
		List<StatusItem> lsi = null;

		Reader rd = new InputStreamReader(new ByteArrayInputStream(
				xml.getBytes()));

		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(rd);

		int eventType = parser.getEventType();

		StatusItem si = null;
		UserItem user = null;
		Boolean isUser = false;

		while (eventType != XmlPullParser.END_DOCUMENT) {
			String node = parser.getName();

			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				lsi = new LinkedList<StatusItem>();
				break;
			case XmlPullParser.START_TAG:
				if (!isUser) {
					if ("status".equals(node))
						si = new StatusItem();
					if ("created_at".equals(node))
						si.setCreatedAt(parser.nextText());
					if ("id".equals(node))
						si.setId(parser.nextText());
					if ("text".equals(node))
						si.setText(parser.nextText());
					if ("source".equals(node))
						si.setSource(parser.nextText());
					if ("truncated".equals(node))
						si.setTruncated(parser.nextText());
					if ("in_reply_to_status_id".equals(node))
						si.setInReplyToStatusId(parser.nextText());
					if ("in_reply_to_user_id".equals(node))
						si.setInReplyToUserId(parser.nextText());
					if ("favorited".equals(node))
						si.setFavorited(parser.nextText());
					if ("in_reply_to_screen_name".equals(node))
						si.setInReplyToScreenName(parser.nextText());
					if ("photo_url".equals(node))
						si.setPhotoUrl(parser.nextText());
					if ("user".equals(node)) {
						isUser = true;
					}
				} else {
					if ("id".equals(node))
						si.getUser().setId(parser.nextText());
					if ("name".equals(node))
						si.getUser().setName(parser.nextText());
					if ("screen_name".equals(node))
						si.getUser().setScreenName(parser.nextText());
					if ("location".equals(node))
						si.getUser().setLocation(parser.nextText());
					if ("description".equals(node))
						si.getUser().setDescription(parser.nextText());
					if ("profile_image_url".equals(node))
						si.getUser().setProfileImageUrl(parser.nextText());
					if ("url".equals(node))
						si.getUser().setUrl(parser.nextText());
					if ("protected".equals(node))
						si.getUser().setIsprotected(parser.nextText());
					if ("followers_count".equals(node))
						si.getUser().setFollowersCount(parser.nextText());
					if ("friends_count".equals(node))
						si.getUser().setFriendsCount(parser.nextText());
					if ("favourites_count".equals(node))
						si.getUser().setFavouritesCount(parser.nextText());
					if ("statuses_count".equals(node))
						si.getUser().setStatusesCount(parser.nextText());
				}
				break;
			case XmlPullParser.END_TAG:
				if ("user".equals(node))
					isUser = false;
				if ("status".equals(node))
					lsi.add(si);
				break;
			default:
				break;
			}
			eventType = parser.next();
		}
		return lsi;
	}

	public static List<UserItem> getUsers(String xml)
			throws XmlPullParserException, IOException {
		Reader rd = new InputStreamReader(new ByteArrayInputStream(
				xml.getBytes()));

		List<UserItem> ui = null;

		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(rd);

		UserItem user = null;

		int eventType = parser.getEventType();

		boolean isStatus = false;

		while (eventType != XmlPullParser.END_DOCUMENT) {
			String node = parser.getName();
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				ui = new LinkedList<UserItem>();
				break;
			case XmlPullParser.START_TAG:
				if (isStatus == false) {
					if ("user".equals(node))
						user = new UserItem();
					if ("id".equals(node))
						user.setId(parser.nextText());
					if ("name".equals(node))
						user.setName(parser.nextText());
					if ("screen_name".equals(node))
						user.setScreenName(parser.nextText());
					if ("location".equals(node))
						user.setLocation(parser.nextText());
					if ("description".equals(node))
						user.setDescription(parser.nextText());
					if ("profile_image_url".equals(node))
						user.setProfileImageUrl(parser.nextText());
					if ("url".equals(node))
						user.setUrl(parser.nextText());
					if ("protected".equals(node))
						user.setIsprotected(parser.nextText());
					if ("followers_count".equals(node))
						user.setFollowersCount(parser.nextText());
					if ("friends_count".equals(node))
						user.setFriendsCount(parser.nextText());
					if ("favourites_count".equals(node))
						user.setFavouritesCount(parser.nextText());
					if ("statuses_count".equals(node))
						user.setStatusesCount(parser.nextText());
					if ("status".equals(node))
						isStatus = true;
				}
				break;
			case XmlPullParser.END_TAG:
				if ("user".equals(node))
					ui.add(user);
				if ("status".equals(node))
					isStatus = false;
				break;
			default:
				break;
			}
			eventType = parser.next();
		}
		return ui;
	}

	public static List<String> getIds(String xml)
			throws XmlPullParserException, IOException {
		Reader rd = new InputStreamReader(new ByteArrayInputStream(
				xml.getBytes()));

		List<String> ls = null;

		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(rd);

		UserItem user = null;

		int eventType = parser.getEventType();

		while (eventType != XmlPullParser.END_DOCUMENT) {
			String node = parser.getName();
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				ls = new LinkedList<String>();
			case XmlPullParser.START_TAG:
				if ("id".equals(node))
					ls.add(parser.nextText());
			case XmlPullParser.END_TAG:
			default:
				break;
			}
			eventType = parser.next();
		}
		return ls;
	}

}
